package com.guigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.MenuRoleMapper;
import com.guigu.mapper.RolesMapper;
import com.guigu.mapper.SystemUsersMapper;
import com.guigu.mapper.SystemuUserRoleMapper;
import com.guigu.pojo.SystemUsers;
import com.guigu.pojo.SystemuUserRole;
import com.guigu.service.SystemUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, SystemUsers> implements SystemUsersService {

    @Autowired
    SystemUsersMapper systemUsersMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;

    @Autowired
    SystemuUserRoleMapper systemuUserRoleMapper;

    @Autowired
    RolesMapper rolesMapper;

    //后端登录
    @Override
    public SystemUsers adminlogin(SystemUsers systemUsers) {

        QueryWrapper<SystemUsers> queryWrapper
                = new QueryWrapper<SystemUsers>();

        queryWrapper.eq("su_name",systemUsers.getSuName());

        queryWrapper.eq("su_pwd",systemUsers.getSuPwd());

        queryWrapper.eq("account_state",0);

        return systemUsersMapper.selectOne(queryWrapper);
    }

    //分页获取所有员工
    @Override
    public Page<SystemUsers> queryallUsers(Integer pageno, Integer pagesize, SystemUsers systemUsers) {

        QueryWrapper<SystemUsers> queryWrapper =
                new QueryWrapper<SystemUsers>();

            if (!StringUtils.isEmpty(systemUsers.getSuName())){
                queryWrapper.like("su_name",systemUsers.getSuName());
            }


        Page<SystemUsers> page = new Page<SystemUsers>(pageno,pagesize);

        page.setPages(pageno);
        page.setSize(pagesize);

        return systemUsersMapper.selectPage(page,queryWrapper);
    }

    //修改员工的状态
    @Override
    public Map alterSystembyid(SystemUsers systemUsers) {

        Map map = new HashMap();

        map.put("msg","冻结失败");

       SystemUsers systemUsers1 = systemUsersMapper.selectById(systemUsers.getId());

       if (systemUsers1.getAccountState()==0){
               systemUsers1.setAccountState(1);
               systemUsersMapper.updateById(systemUsers1);
               map.put("msg","成功");
       }else {
               systemUsers1.setAccountState(0);
               systemUsersMapper.updateById(systemUsers1);
               map.put("msg","成功");
       }


        return map;
    }

    //员工添加
    @Override
    public Map addbySystem(Integer roleName,SystemUsers systemUsers, MultipartFile img, HttpServletRequest request) {

        Map map = new HashMap();
        map.put("msg","添加失败");
        systemUsers.setAccountState(0);
        //判断是否上传图片
        if (img!=null){
            String pathName = request.getServletContext().getRealPath("/syUserimgs"); //路径名
            File file = new File(pathName);
            if (!file.exists()) {
                //不存在就创建
                file.mkdirs();
            }
            //获取文件名称
            String fileName = img.getOriginalFilename();
            //保存文件到路径
            try {
                img.transferTo(new File(pathName, fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //将路径设置到对象
            systemUsers.setTopImg("syUserimgs/" + fileName);
        }
            System.out.println("----------"+systemUsers);

        QueryWrapper<SystemUsers> queryWrapper
                        = new QueryWrapper<>();

        if (systemUsersMapper.insert(systemUsers)>0){
            queryWrapper.eq("su_name",systemUsers.getSuName());
            queryWrapper.eq("su_pwd",systemUsers.getSuPwd());
            SystemUsers systemUsers1=systemUsersMapper.selectOne(queryWrapper);

            SystemuUserRole systemuUserRole = new SystemuUserRole();
            systemuUserRole.setRId(roleName);
            systemuUserRole.setSuId(systemUsers1.getId());
            systemuUserRoleMapper.insert(systemuUserRole);
            map.put("msg","添加成功");
        }


        return map;
    }

    //根据id查询
    @Override
    public SystemUsers querybyid(SystemUsers systemUsers) {

        SystemUsers systemUsers1 = systemUsersMapper.selectById(systemUsers.getId());


        systemUsers1.setRoles(rolesMapper.queryrole(systemUsers.getId()));

        return systemUsers1;
    }

    //修改
    @Override
    public Map editStaff(SystemUsers systemUsers, MultipartFile img,Integer roleId, HttpServletRequest request) {
       Map map =new HashMap();
       map.put("msg","修改失败");


        //判断是否上传图片
        if (img!=null){
            String pathName = request.getServletContext().getRealPath("/syUserimgs"); //路径名
            File file = new File(pathName);
            if (!file.exists()) {
                //不存在就创建
                file.mkdirs();
            }
            //获取文件名称
            String fileName = img.getOriginalFilename();
            //保存文件到路径
            try {
                img.transferTo(new File(pathName, fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //将路径设置到对象
            systemUsers.setTopImg("syUserimgs/" + fileName);
        }
            if (systemUsers.getTopImg()!=null && systemUsers.getTopImg()!=""){
                systemUsersMapper.updateById(systemUsers);
            }

            QueryWrapper<SystemuUserRole> queryWrapper1
                    = new  QueryWrapper<SystemuUserRole>();

            if (roleId!=null) {
                queryWrapper1.eq("su_id",systemUsers.getId());

                SystemuUserRole systemuUserRole =new SystemuUserRole();
                systemuUserRole.setRId(roleId);

                systemuUserRoleMapper.update(systemuUserRole,queryWrapper1);

            }

                map.put("msg","修改成功");

       return map;
    }


    //判断重复
    @Override
    public Map querybyname(SystemUsers systemUsers) {
        Map map = new HashMap();
        map.put("msg","重复");
        QueryWrapper<SystemUsers> queryWrapper =
                new QueryWrapper<SystemUsers>();

        queryWrapper.eq("su_name",systemUsers.getSuName());

       if (systemUsersMapper.selectOne(queryWrapper)==null){
           map.put("msg","不重复");
       }

        return map;
    }


    //修改密码
    @Override
    public Map editPwd(SystemUsers systemUsers) {
        Map map =new HashMap();
        map.put("msg","修改失败");

        if (systemUsersMapper.updateById(systemUsers)>0){
            map.put("msg","修改成功");
        }

        return map;
    }
}
